package com.shop.shop_end.controller;
import com.shop.shop_end.dto.LoginUser;
import com.shop.shop_end.dto.RegisterUser;
import com.shop.shop_end.entity.MessageJson;
import com.shop.shop_end.entity.User;
import com.shop.shop_end.service.AdminService;
import com.shop.shop_end.service.MessageJsonService;
import com.shop.shop_end.service.UserService;
import com.shop.shop_end.service.impl.AdminServiceImpl;
import com.shop.shop_end.utils.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {
    final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Resource
    private UserService userService;

    @Resource
    private MessageJsonService messageJsonService;

    /**
     * 注册
     * @param registerUser
     * @return
     */
    @RequestMapping("/register")
    public Result userRegister(@RequestBody RegisterUser registerUser){
        logger.info("用户【{}】开始注册", registerUser.getUsername());
        return userService.register(registerUser);
    }

    /**
     * 登录
     * @param loginUser
     * @return
     */
    @RequestMapping("/login")
    public Result userLogin(@RequestBody LoginUser loginUser) {
        logger.info("用户【{}】开始登录", loginUser.getUsername());
        return userService.login(loginUser.getUsername(), loginUser.getPassword());
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @RequestMapping("/updateUserById")
    public Result updateUserById(@RequestBody User user){
        logger.info("用户【{}】正在修改信息", user.getUsername());
        return userService.updateUserById(user);
    }


    @RequestMapping("/addOneMessage")
    public Result addOneMessage(@RequestBody MessageJson messageJson){
        logger.info("增加一条消息到数据库");
        //晚点考虑是否要用到redis
        return messageJsonService.addOneMsg(messageJson);
    }

    /**
     * 获取uid为myId，mid为friId或uid为friId，mid为myId的聊天记录
     * @param myId
     * @param friId
     * @return
     */
    @RequestMapping("/getChatContentById/{myId}/{friId}")
    public Result getChatContentById(@PathVariable("myId") Long myId, @PathVariable("friId") Long friId){
        logger.info("获取我和好友的聊天记录");
        return messageJsonService.getMyMsg(myId, friId);
    }

    /**
     * 根据关键字来搜索用户
     * @param keyword 关键字
     * @return
     */
    @RequestMapping("/getAllUserByKeyword/{keyword}")
    public Result getAllUserByKeyword(@PathVariable("keyword") String keyword){
        logger.info("根据关键字搜索用户，keyword==>" + keyword);
        return userService.getAllUserByKeyword(keyword);
    }

    @RequestMapping("/deleteUserById/{id}")
    public Result deleteUserById(@PathVariable("id") Long id){
        return userService.deleteUserById(id);
    }
}
